﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleData.ObjectModel.Design;
using SimpleData.ObjectModel;

namespace SimpleData
{
	public static class SchemaDataGenExtensions
	{


		public static DataGenEntityBuilder<Schema> Schema(this DGen builder )
		{
			return builder.Schema( builder.GetRandomName( "schema" ) );
		}

		public static DataGenEntityBuilder<Schema> Schema( this DGen builder, string name )
		{
			var context = new DataAccess.SqlClient.DataObjectContext();
			var schema = new Schema( name, "Tests" );

			context.CreateObjectSet<Schema>().AddObject( schema );
			return new DataGenEntityBuilder<Schema>( builder, context, schema );			
		}


		public static DataGenEntityBuilder<Schema> Fields( this DataGenEntityBuilder<Schema> builder, FieldTypeDescriptor type, params string[] fieldNames )
		{
			if ( fieldNames == null || fieldNames.Length == 0 )
				builder.Entity.Fields.Add( new FieldDescriptor( new FieldName( builder.DGen.GetRandomName() ), type ) );
			else
				foreach ( var field in fieldNames )
					builder.Entity.Fields.Add( new FieldDescriptor( new FieldName( field ), type ) { DisplayName = String.Concat( "test ", field ) } );

			return builder;
		}


		public static DataGenEntityBuilder<Schema> WithRules( this DataGenEntityBuilder<Schema> builder )
		{
			return builder
				.WithRules( r =>
					{
						r.ErrorMessageFormat = "Message for " + r.Name.ToString();
						r.ToolTipText = "ToolTipText for " + r.Name.ToString();
					} );
		}

		//public static DataGenEntityBuilder<Schema> WithRules( this DataGenEntityBuilder<Schema> builder, IRuleBuilder ruleBuilder )
		//{
		//    return builder.WithRules( rule => rule.Factory = Runtime.Current.Mocks().RuleBuilderFactory().Create( () => ruleBuilder ).Get() );
		//}

		public static DataGenEntityBuilder<Schema> WithRules( this DataGenEntityBuilder<Schema> builder, Action<SchemaRule> ruleSetup )
		{
			//Rule rule = new Rule( new RuleName( builder.DGen.GetRandomName( "rule" ) ) );
			SchemaRule rule = new SchemaRule( new RuleName( builder.DGen.GetRandomName( "schemaRule" ) ), builder.Entity );
			//SchemaRule rule = new SchemaRule( new RuleName( "schemaRule" ), builder.Entity );

			builder.Entity.Rules.Add( rule );
			if ( ruleSetup != null )
				ruleSetup( rule );
			return builder;
		}
	}
}
